import keras
import tensorflow as tf
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense,Dropout,Flatten,Activation
from keras.layers import Convolution2D as Conv2D
from keras.layers import MaxPooling2D
from keras import backend as K


"""
CNN 的Param参数计算：
Total params: 63,210
Trainable params: 63,210
Non-trainable params: 0
对CNN模型：Param的计算方法：——（卷积核长度 * 卷积核宽度  * 通道数 + 1） * 卷积核个数
（1）第一个Conv层：Conv2D(32,kernel_size = (3,2),input_shape = (8,8,1))
        Param = (3 * 2 * 1 + 1) * 32 = 224
（2）第二个Conv层：Conv2D(64,(2,3),activation = 'relu'))
        经过第一层32个卷积核的作用，第二层输入数据通道数为32；
        Param = (2 * 3 * 32 + 1) * 64
（3）第三个conv层：Conv2D(64,(2,2),activation = 'relu'))
经过第二层64个卷积核的作用，第二层输入数据通道数为64；
Param = (2 * 2 * 64) * 64 = 16648
（4）
"""
model = Sequential()

model.add(Conv2D(32,kernel_size = (3,2),input_shape = (8,8,1)))
convout1 = Activation('relu')
model.add(convout1)

model.add(Conv2D(64,(2,3),activation = 'relu'))
model.add(Conv2D(64,(2,2),activation = 'relu'))
model.add(MaxPooling2D(pool_size = (2,2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128,activation='relu'))
model.add(Dropout(0.5))  #Drouput(控制需要断开的神经元的比例rate)
model.add(Dense(10,activation='softmax'))

model.compile(loss = tf.keras.losses.categorical_crossentropy,
              optimizer = tf.keras.optimizers.Adadelta(),
              metrics = ['accuracy'])
model.summary()

